package basic;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Stack;

public class Chapter8 {

    public Chapter8() {

    }
    public static void main(String[] args) {
        String s = "(])";
        System.out.println(isVaild(s));
    }



    public static boolean isVaild(String s) {
        HashMap<Character, Character> hashmap = new HashMap<Character, Character>();
        hashmap.put(')', '(');
        hashmap.put('}','{');
        hashmap.put(']','[');
        Stack<Character> stack = new Stack<Character>();
        char[] chars = s.toCharArray();
        for(char aChar: chars) {
            if (hashmap.containsKey(aChar)) {
                if(stack.empty()) return false;
                if (stack.peek() == hashmap.get(aChar)) stack.pop();
                else return false;
            } else {
                stack.push(aChar);
            }
        }
        if (stack.isEmpty()){
            return true;
        } else {
            return false;
        }
    }
}
